package com.quyunshuo.androidbaseframemvvm.base.mvvm.v

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.util.Log
import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.PrefsManager.Keys.LANGUAGE
import java.util.*

object LanguageManager {
    // 支持的语种枚举
    enum class Language(val code: String, val displayName: String) {
        SIMPLIFIED_CHINESE("zh", "简体中文"),
        TRADITIONAL_CHINESE("zh-Hant", "繁體中文"),
        ENGLISH("en", "English"),
        JAPANESE("ja", "日本語"),
        KOREAN("ar", "阿拉伯语"),
        SPANISH("es", "Español"),
        FRENCH("fr", "Français"),
        RUSSIAN("ru", "Русский"),
        GERMAN("de", "Deutsch"),
        PORTUGUESE("pt", "Português");

        companion object {
            fun fromCode(code: String): Language {
                return values().firstOrNull { it.code == code } ?: DEFAULT_LANGUAGE
            }

            fun fromPosition(pos: Int): Language {
                return values().getOrElse(pos) { DEFAULT_LANGUAGE }
            }

            val DEFAULT_LANGUAGE = ENGLISH
        }
    }

    // 键值存储的语言键名

    // 初始化并恢复用户选择的语言
    fun initialize(context: Context) {
        val savedLangCode = PrefsManager.read<String>(LANGUAGE, Language.DEFAULT_LANGUAGE.code)
        setAppLanguage(context, savedLangCode)
    }

    // 更改应用语言
    fun setAppLanguage(context: Context, languageCode: String) {
        val language = Language.fromCode(languageCode)
        setAppLanguage(context, language)
    }

    @SuppressLint("ApplySharedPref")
    fun setAppLanguage(context: Context, language: Language) {
        // 保存到Preferences
        PrefsManager.save(LANGUAGE, language.code)

        // 更新应用配置
        updateResources(context, language)
    }

    // 获取当前设置的语言
    fun getCurrentLanguage(): Language {
        val langCode = PrefsManager.read<String>(LANGUAGE, Language.DEFAULT_LANGUAGE.code)
        return Language.fromCode(langCode)
    }

    // 获取支持的所有语言列表
    fun getSupportedLanguages(): List<Language> {
        return Language.values().toList()
    }

    // 更新资源配置
    private fun updateResources(context: Context, language: Language) {
        val locale = Locale.forLanguageTag(language.code)
        Locale.setDefault(locale)

        val resources = context.resources
        val configuration = Configuration(resources.configuration)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(locale)
            configuration.setLocales(LocaleList(locale))
        } else {
            @Suppress("DEPRECATION")
            configuration.locale = locale
        }

        // 使用新的配置更新资源
        resources.updateConfiguration(configuration, resources.displayMetrics)
    }

    // 为特定上下文创建本地化上下文
    fun createLocalizedContext(context: Context): Context {
        val langCode = PrefsManager.read<String>(LANGUAGE, Language.DEFAULT_LANGUAGE.code)
        val locale = Locale.forLanguageTag(langCode)

        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val config = context.resources.configuration
            config.setLocale(locale)
            config.setLocales(LocaleList(locale))
            context.createConfigurationContext(config)
        } else {
            @Suppress("DEPRECATION")
            val config = Configuration(context.resources.configuration)
            config.locale = locale
            context.createConfigurationContext(config)
        }
    }
}